
from WebFramework.homework.wechat_po.utils.log_utils import logger


def ui_exception_record(func):
    def inner(*args, **kwargs):
        instance = args[0]  # self
        # driver = instance.driver  # 获取 driver
        try:
            # 当被装饰方法/函数发生异常就捕获并做数据记录
            return func(*args, **kwargs)
        except Exception:
            logger.debug("出现异常啦~")
            # 截图操作
            instance.get_screen()
            # 记录page_source
            instance.get_page_source()
            raise Exception

    return inner



def click_exception(by, element, max_attempts=5):
    def _inner(driver):
        # 多次点击按钮
        actul_attempts = 0  # 实际点击次数
        while actul_attempts < max_attempts:
            # 进行点击操作
            actul_attempts += 1  # 每次循环，实际点击次数加1
            try:
                # 如果点击过程报错，则直接执行 except 逻辑，并切继续循环
                # 没有报错，则直接return 循环结束
                driver.find_element(by, element).click()
                return True
            except Exception:
                logger.debug("点击的时候出现了一次异常")
        # 当实际点击次数大于最大点击次数时，结束循环并抛出异常
        raise Exception("超出了最大点击次数")

    # return _inner() 错误写法
    return _inner
